﻿/*
创建人：longb
日期：2022-05-20
描述：收料确认js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    editIndex: null, //easyUI单元格编辑
    oldeditIndex: null,
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSReciveMaterialConfirmHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        page.addEditCell();
    },
    addEditCell: function () //给easyuigrid新增editcell方法
    {
        $.extend($.fn.datagrid.methods, {
            editCell: function (jq, param) {
                return jq.each(function () {
                    var opts = $(this).datagrid('options');
                    var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
                    for (var i = 0; i < fields.length; i++) {
                        var col = $(this).datagrid('getColumnOption', fields[i]);
                        col.editor1 = col.editor;
                        if (fields[i] != param.field) {
                            col.editor = null;
                        }
                    }
                    $(this).datagrid('beginEdit', param.index);
                    for (var i = 0; i < fields.length; i++) {
                        var col = $(this).datagrid('getColumnOption', fields[i]);
                        col.editor = col.editor1;
                    }
                });
            }
        });
    },
    endEditing: function (gridId) {
        if (page.editIndex == undefined) { return true }
        if ($('#' + gridId).datagrid('validateRow', page.editIndex)) {
            $('#' + gridId).datagrid('endEdit', page.editIndex);
            page.editIndex = undefined;
            return true;
        } else {
            return false;
        }
    },
    //页面事件绑定
    bind: function () {
        $('#OrderType').combobox({
            data: [{ id: 1, text: '正常' }, { id: 0, text: '临时'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox({
            data: [{ id: 0, text: '未生成' }, { id: 1, text: '已生成'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#OrderType').combobox('setValue', 1);
        $('#Status').combobox('setValue', 1);
        //任务单信息
        $("#gdContainerInfo").datagrid({
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onSelect: function (index, rowData) { page.GetMaterialData(); },
            onUnselect: function (index, rowData) { page.GetMaterialData(); },
            columns: [[
            // { field: 'ck', title: '选择', checkbox: true },
            // {field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 130 }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WFDESC', title: '工艺名称', width: 120 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 130 },
                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 130 },
            //{ field: 'TASKBILLSTATUS', title: '任务单状态', width: 100 },
                 {field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 },
                 { field: 'QTL', title: '预齐套率', width: 90, align: 'center', formatter: function (value, row, index) {
                     //var QT = (row.PMCOUNT == 0 ? 0 : row.FINISHCOUNT / row.PMCOUNT); 
                     if (value >= 1) { return '<span style="color:#0000ff">100%</span>'; } else { return '<span style="color:red">' + (parseInt(value * 100)) + '%</span>'; } 
                 } }
	           ]]
        });

        //物料信息
        $('#MaterialTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            columns: [[
                    { field: 'ck', title: '选择', checkbox: true },
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'PRODUCTDESC', title: '件号名称', width: 200 },
                    { field: 'PARTTYPE', title: '零件类型', width: 100, align: 'center', formatter: function (value, row, index) { if (value == "0") { return '大件'; } else { return '小件'; } } },
		            { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 100, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: 'ERP批次', width: 100, align: 'center' },
                    { field: 'ISSUEQTY', title: '装配数量', width: 100, align: 'center', hidden: true },
                    { field: 'CHANGEQTY', title: '本次收料数量', width: 130, align: 'center', editor: { type: 'numberbox' }, styler: function () { return 'background-color:#f8f8f8;color:red'; }
                    , formatter: function (value, row, index) { if (value < 0) { return 0; } else { return value; } }
                    },
                    { field: 'CONTAINERNAME', title: '批次号', width: 200, align: 'center' },
                    { field: 'ONECONFIRMQTY', title: '已收料数量', width: 90, align: 'center' },
                    { field: 'TOTALCOLLECTQTY', title: '已收料数量', width: 90, align: 'center', hidden: true },
                    { field: 'NOTOTALCOLLECTQTY', title: '未收料数量', width: 90, align: 'center', formatter: function (value, row, index) { var c = row.ISSUEQTY - row.ONECONFIRMQTY; if (c < 0) { return 0; } else { return c; } } },
                    { field: 'CONTAINERNAME1', title: '预齐套率', width: 90, align: 'center', formatter: function (value, row, index) { var c = row.TOTALCOLLECTQTY / row.FIRSTASSEMBLEQTY; if (c >= 1) { return '<span style="color:#0000ff">' + parseInt(c * 100) + '%</span>'; } else { return '<span style="color:red">' + parseInt(c * 100) + '%</span>'; } } },
                    { field: 'OP', title: '操作', width: 100, align: 'center', formatter: function (value, row, index) { return '<a href="javascript:void(0)" class="easyui-linkbutton l-btn" style="text-decoration:none;padding: 0 10px;" onclick="page.BatchReciveMaterialCofirm(\'' + index + '\')">收料确认</a>' } }
                ]],
            onCheck: function (index, rowData) { },
            onUncheck: function (index, rowData) { },
            onCheckAll: function (index, rowData) { },
            onUncheckAll: function (index, rowData) { },
            onClickCell: function (index, field, value) {
                //console.info("------" + index + "-----------" + page.editIndex + "-------" + page.oldeditIndex + "----");
                page.oldeditIndex = page.editIndex;
                if (value > 0 && field == "CHANGEQTY") {
                    if (page.endEditing('MaterialTable')) {
                        $('#MaterialTable').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                } else {
                    if (page.editIndex!=null) { $('#MaterialTable').datagrid('endEdit', page.editIndex); }
                }
            },
            onAfterEdit: function (index, row, changes) {
                if (row.ISSUEQTY < Number(changes.CHANGEQTY) + Number(row.ONECONFIRMQTY)) {
                    $.messager.confirm('提示', '本次收料数量+已收料数量>装配数量，是否继续？', function (r) {
                        if (r)//继续执行
                        {
                            $('#MaterialTable').datagrid('updateRow', { index: page.oldeditIndex, row: { CHANGEQTY: changes.CHANGEQTY} });
                        } else {
                            $('#MaterialTable').datagrid('updateRow', { index: page.oldeditIndex, row: { CHANGEQTY: row.FIRSTASSEMBLEQTY - row.TOTALCOLLECTQTY} });
                        }
                    });
                }
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });


    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        $('#MaterialTable').datagrid('loadData', []);
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#MaterialTable').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTaskOrderData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //                console.info(res);
                page.pagetotal = res.RowCount;
                $('#gdContainerInfo').datagrid('loadData', res.DBTable)
            }
        });
    },
    //物料信息
    GetMaterialData: function () {
        $('#MaterialTable').datagrid('loadData', []);
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var strNoList = [];
        var strIDList = [];
        var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
        $.each(CheckedRows, function (index, item) {
            strNoList.push(item.TOGETHERFLAG);
            strIDList.push(item.CONTAINERID);
        })
        var param = {
            action: "GetMaterialData",
            strNoList: strNoList.join(','),
            strIDList: strIDList.join(','),
            data: JSON.stringify([data])
        };
        if (strNoList.length == 0) {
            //displayMessage("请选择任务单");
            return;
        }
        page.post(param, function (res) {
            if (res.Result) {
                //                console.info(res);
                $('#MaterialTable').datagrid('loadData', res.DBTable)
            }
            //            displayMessage(res.Message, res.Result);
        });

    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },
    //批量报工
    BatchReciveMaterialCofirm: function (index) {
        var rows = $('#MaterialTable').datagrid('getChecked');
        if (index != null) {
            rows = [$('#MaterialTable').datagrid('getRows')[index]];
        }
        //        console.info(rows);
        var CheckedRows = $('#gdContainerInfo').datagrid('getSelected');
        var param = {
            action: "ReciveMaterialCofirm",
            type: page.pageType,
            data: JSON.stringify(rows),
            CONTAINERID: CheckedRows.CONTAINERID
        };
        if (rows.length == 0) {
            displayMessage("请先选择对应物料信息，再进行确认");
            return;
        }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetMaterialData();
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});